package com.ruoyi.aitu8.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.anotation.ServiceLog;
import com.ruoyi.aitcommon.service.IAitEntityInfoService;
import com.ruoyi.aitcommon.utils.AITException;
import com.ruoyi.aitcommon.utils.EntityCache;
import com.ruoyi.aitcommon.utils.ExceptionUtils;
import com.ruoyi.aitcommon.utils.JSONResult;
import com.ruoyi.aitcommon.utils.ListMapUtils;
import com.ruoyi.aitcommon.utils.LogUtils;
import com.ruoyi.aitcommon.utils.RestServiceUtils;
import com.ruoyi.aitcommon.vo.AggMap;
import com.ruoyi.common.constant.HttpStatus;

/**
 * U8接口的封装类
 * 使用最新的U8内网OPENAPI接口
 * @author Administrator
 *
 */
@Service
public class U8RestService {
	@Autowired
	IAitEntityInfoService infoService;
	@Autowired
	EntityCache entityCache;
	
	@ServiceLog("MAP转换U8实体服务")
	public List<AggMap> transU8Map(List<Map<String,Object>> values
			,String headkey,String fengge) throws Exception{
		return ListMapUtils.transAggVO(values, headkey, fengge);
	}
	
	@ServiceLog("U8实体保存服务")
	public List<JSONObject> entitySave(Map<String,Object> context
			,String url
			,List<Map<String,Object>> datas
			,String entityName
			,JSONObject entityInfo) throws Exception {
		List<JSONObject> retlist=new ArrayList<JSONObject>();
		
		ExceptionUtils.checkBlankOrUndefined(context, "context为空");
		ExceptionUtils.checkBlankOrUndefined(url, "url为空");
		ExceptionUtils.checkBlankOrUndefined(entityInfo, "entityInfo为空");
		
		//从缓存过滤重复数据
		List<Map<String,Object>> filterlist = entityCache.filterEntityChange(entityInfo, datas);
		for (Map<String,Object> data : filterlist) {
//			循环单条处理
			try {
//				调用 U8 接口
				JSONObject retjson=restInvoke(context, url, data,entityName);
				if(entityInfo!=null) {
//					处理实体日志和缓存
					if(!entityInfo.containsKey("oper"))
						entityInfo.put("oper", "保存");
					infoService.insertAitEntityInfo(entityInfo, data, null);
				}
				retlist.add(retjson);
			}catch(Exception ex) {
//				ex.printStackTrace();
//				处理实体日志和缓存-失败信息
				infoService.insertAitEntityInfo(entityInfo, data, ex);
//				处理返回
				JSONObject retjson=JSONResult.error(ex.getMessage());
				retlist.add(retjson);
			}
		}
		return retlist;

	}
	
	@ServiceLog("U8单据保存服务")
	public List<JSONObject> docSave(Map<String,Object> context
			,String url
			,List<AggMap> datas
			,JSONObject entityInfo) throws Exception {
		List<JSONObject> retlist=new ArrayList<JSONObject>();
		
		ExceptionUtils.checkBlankOrUndefined(context, "context为空");
		ExceptionUtils.checkBlankOrUndefined(url, "url为空");
		ExceptionUtils.checkBlankOrUndefined(entityInfo, "entityInfo为空");
		
		//从缓存过滤重复数据
		List<Map<String,Object>> filterlist = entityCache.filterEntityChange(entityInfo, ListMapUtils.agg2Map(datas));
		for (Map<String,Object> data : filterlist) {
//			循环单条处理
			try {
//				构造表单数据
				AggMap aggData=new AggMap(data);
				HashMap<String,Object> voucher=new LinkedHashMap<String,Object>();
				voucher.put("abt_cardnumber", context.get("abt_eftype"));
				voucher.put("abt_ccardname", "");
				voucher.put("abt_xmlns-z", "EF");
				
				HashMap<String,Object> head=new LinkedHashMap<String,Object>();
				head.put("z-row", aggData.getHeadVO());
				voucher.put("head", head);
				
				HashMap<String,Object> body=new LinkedHashMap<String,Object>();
				body.put("z-row", aggData.getBodyVOS());
				voucher.put("body", body);
//				context.put("voucher", voucher);
				
//				调用 U8 接口
				JSONObject retjson=restInvoke(context, url, voucher,"voucher");
				if(entityInfo!=null) {
//					处理实体日志和缓存
					if(!entityInfo.containsKey("oper"))
						entityInfo.put("oper", "保存");
					infoService.insertAitEntityInfo(entityInfo, data, null);
				}
				retlist.add(retjson);
			}catch(Exception ex) {
//				ex.printStackTrace();
//				处理实体日志和缓存-失败信息
				infoService.insertAitEntityInfo(entityInfo, data, ex);
//				处理返回
				JSONObject retjson=JSONResult.error(ex.getMessage());
				retlist.add(retjson);
			}
		}
		return retlist;
	}
	
	private JSONObject restInvoke(Map<String,Object> context, String url
			, Map<String,Object> data,String entityName) 
			throws Exception {
		if (data == null) {
			return JSONResult.success("没有数据");
		}
		
		//构造U8参数
		context.put(entityName, data);
		JSONObject u8data=new JSONObject();
		u8data.put("ufinterface", context);
		
		// 调用U8 OPENAPI
		JSONObject retjson = RestServiceUtils.post(url,null,u8data);
		
		// 处理U8服务返回
		if(!retjson.containsKey("ufinterface"))
			throw new Exception("返回数据错误!");
		if(retjson.getJSONObject("ufinterface").getIntValue("abt_succeed")!=1) {
			String msg=retjson.getJSONObject("ufinterface").getString("abt_dsc");
			throw new Exception("U8调用报错:"+msg);
		}
		JSONObject ret=new JSONObject();
		ret.put("id", retjson.getString("abt_retid"));
		ret.put("code", retjson.getString("abt_retcode"));
		return ret;
		
	}
	
	
}
